﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../../common/jsp/taglibs.jsp"%>
<!-- 
    Copyright (C)  2011  chen wei hua.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 2.0
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled "GNU Free Documentation License".
 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>仓库管理-新建来料单</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<%@ include file="../../common/jsp/config.jsp"%>
<%@ include file="../../common/jsp/validate.jsp"%>
<%@ include file="../../common/jsp/autocomplete.jsp"%>
<script type="text/javascript" src="common/js/calendar/WdatePicker.js" ></script>
<script type="text/javascript" src="common/js/jquery/chinaArea/jquery.fillOptions.js"></script>
<script type="text/javascript">
var rowIndex = 0;
function addLine() {
	rowIndex++;
	var table = $("#materialsList");
	table.find("tr").last().before(
	           '<tr class="odd">'
	           +'<td>'
	           +'<s:textfield id="materialsNos-'+rowIndex+'" name="materialsNames" size="20" cssClass="{required:true,maxlength:20}"/>'
	           +'<s:hidden id="materialsIds-'+rowIndex+'" name="materialsIds" />'
	           +'</td>'
	           +'<td><span id="materialsCategorys-'+rowIndex+'"></span></td>'
	           +'<td><span id="materialsNames-'+rowIndex+'"></span></td>'
	           +'<td><span id="materialsTypes-'+rowIndex+'"></span></td>'
	           +'<td><s:textfield id="materialsBatchNums-'+rowIndex+'" name="materialsBatchNums" size="10" cssClass="{required:true,maxlength:10}" /></td>'
	           +'<td><s:textfield id="materialsNums-'+rowIndex+'" name="materialsNums" size="10" cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount('+rowIndex+')"/></td>'
	           +'<td><select name="materialsUnits" id="materialsUnits-'+rowIndex+'" class="{selected:true}"><option value="-1">-请选择-</option></select></td>'
	           +'<td><s:textfield id="materialsPrices-'+rowIndex+'" name="materialsPrices" size="10" cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount('+rowIndex+')"/></td>'
	           +'<td><s:textfield id="materialsAmounts-'+rowIndex+'" name="materialsAmounts" readonly="true" size="10" value="0" cssClass="inputdisabled"/></td>'
	           +'<td><input type="button" value="删除" class="input1" onclick="deleteRow(this)" /></td>'
	           +'</tr>');
	initMaterialsAutoSelected(rowIndex);
	initMaterialsUnits(rowIndex);
}

function computerTotleAmount() {
	var totleNumber = 0;
	for (var i = 0; i < rowIndex + 1; i++) {
		totleNumber += parseFloat($("#materialsAmounts-" + i).val());
	}
	$("#invoice_totleAmount").val(totleNumber.toFixed(2));
}

function computerAmount(id) {
	var number = $("#materialsNums-" + id).val();
	var prices = $("#materialsPrices-" + id).val();
	var totleValue = number * prices;
	$("#materialsAmounts-" + id).val(totleValue.toFixed(2));
	computerTotleAmount();
}

function deleteRow(button) {
	$(button).parents("tr").remove();
	//rowIndex--;
	computerTotleAmount();
}

function initMaterialsUnits(id) {
	$("#materialsUnits-"+id).fillOptions(
			"queryBaseCodeJOSN.action?codeCategory=001",
			{datatype:"json",text:"name",value:"id",jsonData:"baseCodeList"});
}

function initMaterialsAutoSelected(id) {
	$("#materialsNos-" + id).each(function(i) {
		$(this).autocomplete("queryMaterialsNames.action", {
			width: 300,
			max: 10,
			autoFill: false,
			dataType: "json",
			parse: function(data) {
				var parsed = [];
				var rows = data.materialsList;
				for (var i=0; i < rows.length; i++) {
					var row = rows[i];
					if (row) {
						parsed[parsed.length] = {
							data: row,
							value: row.id,
							result: row.code//最终选择后显示的内容
						};
					}
				}
				return parsed;
			},
			formatItem: function(row, i, max) {
				return row.code + " " + row.categoryName 
				+ " " + row.name + " " + (row.type?row.type:"");//列表中显示的值
			}
		}).result(function(e, row) {
			var target = e.target;
			var id = target.id.split("-")[1];
			$("#materialsIds-" + id).val(row.id);
			$("#materialsCategorys-" + id).html(row.categoryName);
			$("#materialsNames-" + id).html(row.name);
			$("#materialsTypes-" + id).html(row.type);
		});
	});
}

$(document).ready(function() {
	$("#saveReceiveInvoice").validate();
	$("#invoice_supplierName").autocomplete("querySupplierNames.action", {
		width: 300,
		max: 10,
		autoFill: false,
		dataType: "json",
		parse: function(data) {
			var parsed = [];
			var rows = data.supplierList;
			for (var i=0; i < rows.length; i++) {
				var row = rows[i];
				if (row) {
					parsed[parsed.length] = {
						data: row,
						value: row[0],
						result: row[1]//最终选择后显示的内容
					};
				}
			}
			return parsed;
		},
		formatItem: function(row, i, max) {
			return row[1];//列表中显示的值
		}
	}).result(function(e, row) {
		$(e.target).next().val(row[0]);
	});
	initMaterialsAutoSelected(rowIndex);
	initMaterialsUnits(rowIndex);
});
</script>
</HEAD>
<BODY>
    <s:form action="saveReceiveMaterials">
    <div class="subnav" align="left">
           <table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="100%" align="left" height="31">&nbsp;<img src="common/images/title-pk.gif" border="0"  align="absmiddle"/>&nbsp;<span class="subnavtxt1">仓库管理</span>
			&nbsp;<img src="common/images/leftico2.gif" border="0"  align="absmiddle"/>&nbsp;<span class="subnavtxt2">新建来料单</span></td>
		  </tr>
		</table>
      </div>
     <div class="main_1">
       <table width="99%" border="0" cellpadding="5" cellspacing="1" bgcolor="#D3DEEF">
         <tr>
           <td align="left" class="Blue14" bgcolor="#FAFAFA" colspan="2">来料单信息</td>
         </tr>
		<tr>
	      <td bgcolor="#FAFAFA" align="right"><strong>来料单号</strong></td>
	      <td bgcolor="#FAFAFA">
		    <s:textfield name="invoice.receiveNo" size="20" cssClass="{required:true,maxlength:20}"/>
	        <span class="red-text1">*</span>
	      </td>
		</tr>
         <tr>
		    <td width="19%" bgcolor="#FAFAFA" align="right"><strong>来料日期</strong></td>
		    <td width="81%" bgcolor="#FAFAFA">
		      <s:textfield name="invoice.receiveDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" readonly="true" size="10" cssClass="{required:true}"/>
		      <span class="red-text1">*</span>
		    </td>
		 </tr>
		<tr>
	      <td bgcolor="#FAFAFA" align="right"><strong>供应商名称</strong></td>
	      <td bgcolor="#FAFAFA">
			<s:textfield id="invoice_supplierName" name="invoice.supplierName" size="30" cssClass="{required:true,maxlength:60}"/>
			<s:hidden id="invoice_supplierId" name="invoice.supplierId" />
			<span class="red-text1">*</span>
		  </td>
		</tr>
         <tr>
		    <td bgcolor="#FAFAFA" align="right"><strong>总金额</strong></td>
		    <td bgcolor="#FAFAFA">
				<s:textfield id="invoice_totleAmount" name="invoice.totleAmount" readonly="true" value="0" size="10" cssClass="inputdisabled"/>
			</td>
		 </tr>
       </table>
     </div>
     <div class="clear"></div>
	 <div class="main_1">
       <table width="99%" cellpadding="0" cellspacing="1" border="0" class="ksglxx2a" id="materialsList">
        <tr class="ksglxx2bg">
          <th width="10%" >编码</th>
          <th width="10%" >分类</th>
          <th width="10%" >名称</th>
          <th width="20%" >规格</th>
          <th width="10%" >批号</th>
		  <th width="8%" align="left">数量</th>
		  <th width="8%" align="left">单位</th>
		  <th width="8%" align="left">价格</th>
		  <th width="8%" align="left">金额</th>
		  <th align="left">&nbsp;</th>
		</tr>
        <tr class="odd">
          <td>
		       <s:textfield id="materialsNos-0" name="materialsNames" size="20" cssClass="{required:true,maxlength:20}"/>
		       <s:hidden id="materialsIds-0" name="materialsIds" />
          </td>
          <td><span id="materialsCategorys-0"></span></td>
          <td><span id="materialsNames-0"></span></td>
          <td><span id="materialsTypes-0"></span></td>
          <td><s:textfield id="materialsBatchNums-0" name="materialsBatchNums" size="10" cssClass="{required:true,maxlength:10}" /></td>
          <td>
    		<s:textfield id="materialsNums-0" name="materialsNums" size="10" 
					cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount(0)"/></td>
          <td><select name="materialsUnits" id="materialsUnits-0" class="{selected:true}" /></td>
		  <td><s:textfield id="materialsPrices-0" name="materialsPrices" size="10" 
					cssClass="{required:true,maxlength:10,number:true}" onblur="computerAmount(0)"/></td>
		  <td><s:textfield id="materialsAmounts-0" name="materialsAmounts" readonly="true" size="10" value="0" cssClass="inputdisabled"/></td>
          <td></td>
	    </tr>
        <tr>
         <td colspan="3" align="left" valign="top">
           <input type="button" value="添加" class="input1" onclick="addLine()" />
         </td>
        </tr>
       </table>
      </div>
     <div class="clear"></div>
	 <div class="main_1">
		<table width="98%" border="0" cellpadding="5" cellspacing="0" class="gr-text1">
		   <tr>
			 <td width="45%" align="right">
			   <s:submit value="保存" cssClass="input1" />&nbsp;&nbsp;
			   <input type="button" value="关闭" class="input1" onclick='javascript:window.location.href="receiveInvoiceList.action"' /></td>
			 <td width="55%" align="left"></td>
		   </tr>
	   </table>
	</div>
	<div class="clear"></div>
	</s:form>
</BODY></HTML>
